package net.chery.rm;

import lombok.extern.slf4j.Slf4j;
import net.chery.entity.DataType;
import net.chery.entity.VehProduce;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author mes生产信息映射
 */
@Slf4j
public class MesRowMapper extends VinRowMapper<VehProduce> {
    private final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public MesRowMapper(Set<String> vehModels, Consumer<VehProduce> consumer, Function<String, VehProduce> function) {
        super(vehModels,consumer,function);
    }

    @Override
    public VehProduce mapRow(ResultSet rs, int rowNum) throws SQLException {
        String vin=rs.getString("vin").trim();
        String vehModel=rs.getString("clxh");
        if(!checkData(vin,vehModel)){
            return null;
        }
        VehProduce produce=getNew(vin);
        Date date=parseDate(rs.getString("gmt_modified"),sdf);
        if(produce.getGmtModified()!=null){
            if(date.compareTo(produce.getGmtModified())<0){
                return null;
            }
            produce.setGmtModified(date);
        } else {
            produce.setGmtModified(date);
        }
        produce.setVin(vin);
        produce.setVehModelName(vehModel);
        produce.setVehicleName(rs.getString("clmc"));
        produce.setVehicleBrand(rs.getString("clpp"));
        produce.setOfflineProductionTime(rs.getTimestamp("print_time"));
        produce.setFuelType(rs.getString("rlzl"));
//        produce.setSaleType(rs.getString("sale_type"));
//        produce.setSaleTypeValue(rs.getString("sale_type_value"));
        produce.addType(DataType.MES);
        consumer.accept(produce);
        return produce;
    }
}
